package com.ciaojian.worker.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ciaojian.core.mapper.CouponCodeMapper;
import com.ciaojian.core.model.CouponCode;
import com.ciaojian.core.model.CouponCodeTakeRecord;
import com.ciaojian.core.service.CouponCodeService;
import com.ciaojian.core.util.LogUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * @author Atlakyn
 */
@Service
public class CouponCodeServiceImpl extends CouponCodeService<CouponCodeMapper, CouponCode> implements IService<CouponCode> {
    /**
     * 获取用户特定的优惠券
     *
     * @param userId 用户ID
     * @return /
     */
    public List<Map<String, Object>> getUserCouponCode(Integer userId) {
        return this.baseMapper.getSpecialUserCouponCode(userId);
    }

    @Override
    public Boolean checkNotTook(Integer userId, Integer couponId, Integer activityId) {
        // 判断用户是否领取过券
        QueryWrapper<CouponCodeTakeRecord> queryWrapper = new QueryWrapper<CouponCodeTakeRecord>()
                .eq("user_id", userId)
                .eq("activity_id", activityId)
                .eq("coupon_id", couponId)
                .last("limit 1");
        Integer count = new CouponCodeTakeRecord().selectCount(queryWrapper);
        LogUtil.writeBusinessErrorLog(">>>checkTook>>>用户={},活动={},count={}", userId, activityId, count);
        return count == 0;
    }
}
